//
//  HMPictureView.swift
//  WeiBo
//
//  Created by 李立达 on 15/12/11.
//  Copyright © 2015年 heima11. All rights reserved.
//

import UIKit
import SnapKit

private let itemMargin: CGFloat = 5
let HMStatusCellMargin: CGFloat = 10
private let itemWH = CGFloat(Int(SCREENW - itemMargin*2 - HMStatusCellMargin*2) / 3)

let HMStatusPictureViewItemId : String = "Cell IDHMStatusPictureViewItemId"
class HMPictureView: UICollectionView {

    var HMPicturconst : Constraint?
    
    var pic_urls : [HMPictureInfo]?{
    
        didSet{
            let size = caclz((pic_urls?.count)!)
            
            self.HMPicturconst?.uninstall()
            self.snp_makeConstraints { (make) -> Void in
                
                self.HMPicturconst = make.size.equalTo(size).constraint
            }
            reloadData()
        }
    
    }
    
    lazy var contenLable :UILabel = UILabel(color: UIColor.blackColor(), font: 17)
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        SteupUI()
        
        registerClass(HMPictureCollectionViewCell.self, forCellWithReuseIdentifier: HMStatusPictureViewItemId)
        
        dataSource = self
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    func caclz(count: Int)->CGSize{
    
        let col = count == 4 ? 2 : (count >= 3 ? 3 : count)
        
        let row = count == 4 ? 2 : ((count - 1 ) / 3 + 1)
        
        let width = CGFloat(col) * itemWH + CGFloat(col-1) * itemMargin
        let heiht = CGFloat(row) * itemWH + CGFloat(row-1) * itemMargin
        
        
        return CGSize(width: width, height: heiht)
    }
   
    func SteupUI(){
        backgroundColor = UIColor(white: 0.8, alpha: 1)
        }
    
    
}
extension HMPictureView: UICollectionViewDataSource {
    
    // 返回有多少个cell
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    
    
    // 返回cell
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(HMStatusPictureViewItemId, forIndexPath: indexPath) as! HMPictureCollectionViewCell
        cell.pictureInfo = pic_urls![indexPath.row]
        return cell
    }
}


class HMPictureCollectionViewCell: UICollectionViewCell {
    
    
    var pictureInfo : HMPictureInfo? {
    
        didSet{
        
               if let usrstring = pictureInfo?.thumbnail_pic
               {
                
        gitpicture.hidden = !usrstring.hasSuffix(".gif")
                pictureImage.sd_setImageWithURL(NSURL(string: usrstring), placeholderImage: UIImage(named: "timeline_image_placeholder"))
                }
                }
        
    }
    
    lazy var   pictureImage : UIImageView = {
    
        let imageview = UIImageView()
        
        imageview.contentMode = UIViewContentMode.ScaleAspectFill
        
        imageview.clipsToBounds = true
        
        return imageview
    
    }()
    
    lazy var  gitpicture : UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
    
        addSubview(pictureImage)
        
        addSubview(gitpicture)
        
        pictureImage.snp_makeConstraints { (make) -> Void in
        make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        gitpicture.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureImage)
            make.trailing.equalTo(pictureImage)
        }
    }
    
}